package com.mht2017.common.dto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.mht2017.common.api.Stringify;
import com.mht2017.common.dto.transformer.IndexTopicDtoTransformer;
import com.mht2017.common.utils.SizeUtil;
import org.apache.commons.collections.CollectionUtils;

import com.alibaba.fastjson.JSON;

/**
 * @author yutong.xiao
 * @date 2017年2月27日 下午3:31:53
 */
public class IncInstanceDto implements Stringify {

	private int id;
	private String name;
	private String host;
	private int port;
	private int preQueueSize;
	private String groupId;
	private boolean indexDistribution;
	private List<IndexTopicDto> indexList;

	private long registerTime;
	private long lastHeartbeatTime = -1;
	private boolean alive;

	public IncInstanceDto() {
	}

	public IncInstanceDto(String host, int port) {
		this.host = host;
		this.port = port;
	}

	@Override
	public String toShortString() {
		return String.format("%s-%s-%s:%s/%s,%s-%s,%s", id, host, groupId, port, groupId, registerTime, lastHeartbeatTime, alive);
	}

	public String toShortIndexString() {
		List<String> indexStrings = new ArrayList<>(SizeUtil.sizeZeroIfNull(indexList));
		if (CollectionUtils.isNotEmpty(indexList)) {
			for (IndexTopicDto indexTopicDto : indexList) {
				indexStrings.add(String.format("%s(%s)", indexTopicDto.getIndex(), SizeUtil.size(indexTopicDto.getTopics())));
			}
		}
		return String.format("(%s)%s", indexStrings.size(), indexStrings.toString());
	}

	public String toTopicString() {
		Collection<String> topics = IndexTopicDtoTransformer.pickTopics(indexList);
		return String.format("(%s)%s", topics.size(), topics.toString());
	}

	@Override
	public String toLongString() {
		return toString();
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(long registerTime) {
		this.registerTime = registerTime;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public boolean isAlive() {
		return alive;
	}

	public void setAlive(boolean isAlive) {
		alive = isAlive;
	}

	public long getLastHeartbeatTime() {
		return lastHeartbeatTime;
	}

	public void setLastHeartbeatTime(long lastHeartbeatTime) {
		this.lastHeartbeatTime = lastHeartbeatTime;
	}

	public List<IndexTopicDto> getIndexList() {
		return indexList;
	}

	public void setIndexList(List<IndexTopicDto> indexList) {
		this.indexList = indexList;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public int getPreQueueSize() {
		return preQueueSize;
	}

	public void setPreQueueSize(int preQueueSize) {
		this.preQueueSize = preQueueSize;
	}

	public boolean isIndexDistribution() {
		return indexDistribution;
	}

	public void setIndexDistribution(boolean indexAllocated) {
		indexDistribution = indexAllocated;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		IncInstanceDto other = (IncInstanceDto) obj;
		if (id != other.id) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}

}
